<?php
namespace app\jiyun\service;


use app\common\model\jiyun\OrderInfoExpressModel as JyOrderExpressModel;
use app\common\model\jiyun\OrderModel as JyOrderModel;
use app\common\model\jiyun\OrderPackingModel as JyOrderPackingModel;
use app\common\model\shop\OrderInfoModel as ShopOrderInfoModel;
use app\common\model\shop\OrderModel as ShopOrderModel;
use think\facade\Db;


class SyncOrderService extends BaseService
{



    /**
     * 商品入库
     * @param int $id 物流单id数组
     * @return void
     */
    public function insertStore ( int $id)
    {
        $express_obj = JyOrderExpressModel::mk()->where('id', $id)->find();

        // 现在是 根据回填的 单号来找 信息的。
        // 如果用回填物流单来找信息
        // whereRaw('find_in_set("' . $express_obj->other_order_no . '", other_order_no)')
        // 需要改为  whereRaw('find_in_set("' . $express_obj->express_no . '", express_no)')
        $order_info = ShopOrderInfoModel::mk()->field('id,num,has_num, order_id,goods_name,goods_nu,other_order_no')->whereRaw('find_in_set("' . $express_obj->other_order_no . '", other_order_no)')->where('in_store', ShopOrderInfoModel::SYNC_STATUS_YES)->whereRaw('num <> has_num')->select();

        foreach ($order_info as $info) {

            if ($express_obj->surplus_num < 0) {
                break;
            }
            // 找到头表 拿相关数据
            $field_arr = 'id,platform_id,shop_account_id, account_id, warehouse_id, shop_id, order_id, has_goods_num, order_no,goods_num';
            $order_obj = $order_obj_arr[$info->order_id] ?? JyOrderModel::mk()->field($field_arr)->where('order_id', $info->order_id)->find();

            $num  = ($express_obj->surplus_num > ($info->num - $info->has_num) ) ? ($info->num - $info->has_num) : $express_obj->surplus_num;
            $data = [
                'platform_id'       => $order_obj->platform_id,//平台id(例：Ozone)
                'account_id'        => $order_obj->account_id,//公司ID
                'shop_account_id'   => $order_obj->shop_account_id,//公司ID
                'warehouse_id'      => $order_obj->warehouse_id, // '仓库id'
                'shop_id'           => $order_obj->shop_id,
                'order_id'          => $order_obj->order_id,
                'order_no'          => $order_obj->order_no,
                'order_info_id'     => $info->id,
                'goods_name'        => $info->goods_name,
                'goods_sku'         => $info->goods_nu,
                'jiyun_order_id'    => $order_obj->id,
                'other_order_no'    => $info->other_order_no,
                'num'               => $num,
                'location_code'     => $express_obj->location_code, // 货位
                'express_name'      => $express_obj->express_name, //物流名称
                'status'            => 0, //待集包
                //'flag'              => //substr($express_obj->location_code, 1, 1).'', //待集包
            ];

            $id = JyOrderPackingModel::mk()->insertGetId($data);
           /* $flag = substr($express_obj->location_code, 1, 1).
            JyOrderPackingModel::mk()->where('id', $id)->update(['flag'=>]);*/
            // 更新订单详情已到货数据
            $info->has_num += $num;
            $info->save();
            // 更新包裹单（1688） 当前商品已经 用了多少
            $express_obj->surplus_num -= $num;
            $express_obj->save();
            // 更新集运订单头表已经到货多少个了 ，（1688） 当前商品已经 用了多少
            $order_obj->has_goods_num += $num;

            if ($order_obj->has_goods_num == $order_obj->goods_num) {
                $order_obj->status = 1;
            }

            $order_obj->save();
            // 更新缓存
            $order_obj_arr[$info->order_id] = $order_obj;
        }



    }

}

?>